package com.ruoyi.web.controller.supermarket;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.supermarket.domain.Goods;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supermarket.domain.Commodity;
import com.ruoyi.supermarket.service.ICommodityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品信息Controller
 * 
 * @author lxl
 * @date 2024-07-21
 */
@Api(tags = "商品")
@RestController
@RequestMapping("/commodity/commodity")
public class CommodityController extends BaseController
{
    @Autowired
    private ICommodityService commodityService;

    /**
     * 查询商品信息列表
     */
    @ApiOperation(value = "商品信息列表")
//    @PreAuthorize("@ss.hasPermi('commodity:commodity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Commodity commodity)
    {
        startPage();
        List<Commodity> list = commodityService.selectCommodityList(commodity);
        return getDataTable(list);
    }
    @ApiOperation(value = "商品信息列表-无权限校验")
//    @PreAuthorize("@ss.hasPermi('commodity:commodity:list')")
    @GetMapping("/listNoAuth")
    public TableDataInfo listNoAuth(Commodity commodity)
    {

        List<Commodity> list = commodityService.selectCommodityList(commodity);
        return getDataTable(list);
    }
    /**
     * 导出商品信息列表
     */
    @ApiOperation(value = "导出商品信息列表")
//    @PreAuthorize("@ss.hasPermi('commodity:commodity:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Commodity commodity)
    {
        List<Commodity> list = commodityService.selectCommodityList(commodity);
        ExcelUtil<Commodity> util = new ExcelUtil<Commodity>(Commodity.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @ApiOperation(value = "获取商品信息详细信息")
    @PreAuthorize("@ss.hasPermi('commodity:commodity:query')")
    @GetMapping(value = "/{cid}")
    public AjaxResult getInfo(@PathVariable("cid") Long cid)
    {
        return success(commodityService.selectCommodityByCid(cid));
    }

    /**
     * 新增商品信息
     */
    @ApiOperation(value = "新增商品信息")
    @PreAuthorize("@ss.hasPermi('commodity:commodity:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Commodity commodity)
    {
        return toAjax(commodityService.insertCommodity(commodity));
    }

    /**
     * 修改商品信息
     */
    @ApiOperation(value = "修改商品信息")
    @PreAuthorize("@ss.hasPermi('commodity:commodity:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Commodity commodity)
    {
        return toAjax(commodityService.updateCommodity(commodity));
    }

    /**
     * 删除商品信息
     */
    @ApiOperation(value = "删除商品信息")
    @PreAuthorize("@ss.hasPermi('commodity:commodity:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cids}")
    public AjaxResult remove(@PathVariable Long[] cids)
    {
        return toAjax(commodityService.deleteCommodityByCids(cids));
    }

    @ApiOperation(value = "商品分类")
    @PreAuthorize("@ss.hasPermi('commodity:commodity:category')")
    @GetMapping("/category")
    public TableDataInfo category(Commodity commodity)
    {
//        commodity.setGoods(goods);
       // startPage();
        List<Commodity> list = commodityService.category(commodity);
        return getDataTable(list);

    }

        /**
         * 获取商品订单销售统计信息
         *
         * @return 包含销售统计信息的AjaxResult
         */
        @PreAuthorize("@ss.hasPermi('commodity:commodity:salecount')")
        @GetMapping("/salecount")
        public AjaxResult salecount() {
            return success(commodityService.salecount());
        }

    /**
     * 获取商品订单销售额统计信息
     *
     * @return 包含销售统计信息的AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('commodity:commodity:salegetcount')")
    @GetMapping("/salegetcount")
    public AjaxResult salegetcount() {
        return success(commodityService.salegetcount());
    }

    /**
     * 获取商品进出货统计信息
     *
     * @return 包含进出货统计信息的AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('commodity:commodity:SupplyAndDemand')")
    @GetMapping("/SupplyAndDemand")
    public AjaxResult SupplyAndDemand() {
        return success(commodityService.SupplyAndDemand());
    }


    }


